import { NextApiRequest, NextApiResponse } from 'next';
import prisma from '../../../lib/prisma';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  if (req.method === 'POST') {
    const { userId, password }: { userId: string; password: string } = req.body;

    // console.log('Received request body:', req.body);

    try {
      // 检查用户是否已存在
      const existingUser = await prisma.user.findFirst({ where: { userId } });
      if (existingUser) {
        return res.status(201).json({registerStatus:false, message: '用户已存在' });
      }
      // 插入新用户
      const newUser = await prisma.user.create({ data: { userId, password,score:0 } });
      // console.log('New user created:', newUser);
      return res.status(201).json({registerStatus:true, message: '用户注册成功' });
    } catch (error) {
      return res.status(500).json({ message: '内部服务器错误' });
    }
  } else {
    res.setHeader('Allow', ['POST']);
    res.status(405).end(`不允许的方法: ${req.method}`);
  }
}